﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.Design;
using System.Web.UI;
using System.IO;
using System.Web.UI.WebControls;

namespace Facebook_Graph_Toolkit {
    class LikeBoxDesigner : ControlDesigner{
        LikeBox _LikeBox;
        public override void Initialize(System.ComponentModel.IComponent component) {
            _LikeBox = (LikeBox)component;
            base.Initialize(component);
        }
        public override string GetDesignTimeHtml() {
            MemoryStream MS = new MemoryStream(100 * 1024);
            StreamWriter SW = new StreamWriter(MS);
            HtmlTextWriter HTW = new HtmlTextWriter(SW);

            if (_LikeBox.Width.IsEmpty) HTW.AddStyleAttribute(HtmlTextWriterStyle.Width, "300px");
            else HTW.AddStyleAttribute(HtmlTextWriterStyle.Width, _LikeBox.Width.ToString()+"px");
            if (_LikeBox.BorderColor.IsEmpty) HTW.AddStyleAttribute(HtmlTextWriterStyle.BorderColor, "#1B8BFC");
            else HTW.AddStyleAttribute(HtmlTextWriterStyle.BorderColor, System.Drawing.ColorTranslator.ToHtml(_LikeBox.BorderColor));
            if (_LikeBox.BorderStyle == BorderStyle.NotSet) HTW.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, "solid");
            else HTW.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, _LikeBox.BorderStyle.ToString());
            if (_LikeBox.BorderWidth.IsEmpty) HTW.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "1px");
            else HTW.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, _LikeBox.BorderWidth.ToString() + "px");
            if (string.IsNullOrEmpty(_LikeBox.Font.Name)) HTW.AddStyleAttribute(HtmlTextWriterStyle.FontFamily, "Tahoma");
            else HTW.AddStyleAttribute(HtmlTextWriterStyle.FontFamily, _LikeBox.Font.Name);
            if(_LikeBox.Font.Size.IsEmpty) HTW.AddStyleAttribute(HtmlTextWriterStyle.FontSize, "11px");
            else HTW.AddStyleAttribute(HtmlTextWriterStyle.FontSize, _LikeBox.Font.Size.ToString() + "px");
            HTW.RenderBeginTag(HtmlTextWriterTag.Div);

            if (_LikeBox.LikeBoxMode != LikeBoxMode.FeedOnly) OutputHeader(HTW);
            if (_LikeBox.LikeBoxMode != LikeBoxMode.HeaderOnly) OutputFeed(HTW);

            HTW.RenderEndTag();

            HTW.Flush();
            SW.Flush();
            string html = Encoding.UTF8.GetString(MS.ToArray());
            return html;
        }
        static string dummytext = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";
        private void OutputHeader(HtmlTextWriter output) {
            HtmlTextWriter H = new HtmlTextWriter(output.InnerWriter);
            H.AddStyleAttribute(HtmlTextWriterStyle.BackgroundColor, "#DFF1FF");
            _LikeBox.HeaderStyle.AddAttributesToRender(H);
            H.AddStyleAttribute(HtmlTextWriterStyle.Width, "100%");


            H.RenderBeginTag(HtmlTextWriterTag.Div);

            H.Write(String.Format("<div style=\"padding:{0}px\">", _LikeBox.Padding) +
                "<table><tr>");
            if (_LikeBox.ShowLogo) H.Write("<td><div style=\"width:50px;height:50px;background-color:#fff;text-align:center;font-size:44px;\">?</div></td>");
            H.Write("<td style=\"width:100%\">");

            Style DefaultHeaderCaptionStyle = new Style();
            DefaultHeaderCaptionStyle.Font.Size = FontUnit.Parse("150%");
            _LikeBox.HeaderCaptionStyle.MergeWith(_LikeBox.LinkStyle);
            _LikeBox.HeaderCaptionStyle.MergeWith(DefaultHeaderCaptionStyle);

            //H.AddAttribute("href", FacebookPage.link);
            H.EnterStyle(_LikeBox.HeaderCaptionStyle, HtmlTextWriterTag.A);
            //WriteLink((string)MetadataJO["name"], (string)MetadataJO["link"], H);
            H.Write("[Page Name]");
            H.ExitStyle(_LikeBox.HeaderCaptionStyle, HtmlTextWriterTag.A);

            H.Write(" on Facebook");
            H.Write("</td></tr></table></div>");

            H.RenderEndTag();
        }
        private void OutputFeed(HtmlTextWriter output) {
            output.Write(String.Format("<div style=\"width:100%; height:{0}px; overflow:scroll; overflow-x:hidden;\">", _LikeBox.FeedHeight));
            output.Write(String.Format("<div style=\"padding:{0}px\">", _LikeBox.Padding));
            for (int i = 0; i < 3; i++) {
                if (_LikeBox.ShowTitleInFeed) {
                    WriteLink("[Page Name]", "", output);
                    output.Write(" ");
                }
                output.Write(dummytext + "<br />");
                if (i == 0) WriteLinkAttachment("[link name]", "", "[link caption]", "Lorem Ipsum is simply dummy text of the printing and typesetting industry.", output);
                output.EnterStyle(_LikeBox.DateStyle);
                output.Write(DateTime.Now.ToShortDateString());
                output.ExitStyle(_LikeBox.DateStyle);
                if (i != 10 - 1) output.Write("<hr />");
            }
            output.Write("</div>");
            output.Write("</div>");

            //output.Write(JsonHelper.JsonToHtml(PostJO));
        }
        private void WriteLink(string name, string href, HtmlTextWriter output) {
            output.AddAttribute("href", href);
            output.EnterStyle(_LikeBox.LinkStyle, HtmlTextWriterTag.A);
            output.Write(name);
            output.ExitStyle(_LikeBox.LinkStyle, HtmlTextWriterTag.A);

        }
        private void WriteLinkAttachment(string name, string link, string caption, string description, HtmlTextWriter output) {
            output.Write("<div style=\"margin-left:10px\">");
            WriteLink(name, link, output);
            output.Write("<br />");
            //html += FormatLink(name, link, output) + "<br />";
            output.EnterStyle(_LikeBox.AttachmentStyle);
            output.Write(caption);
            output.Write("<br />");
            output.Write(description);
            output.ExitStyle(_LikeBox.AttachmentStyle);
            output.Write("</div>");
            //html += "<span class=\"LikeBox_Attachment\">" + caption + "</span><br />";
            //html += "<span class=\"LikeBox_Attachment\">" + description + "</span><br />";
            //html += "</div>";
        }
    }
}
